<?php

class CustomerController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function addAction()
    {
    	$form = new Application_Form_Customer();
    	$form->submit->setLabel('Add');
    	$this->view->form = $form;
    	$this->view->phone_checker = true;
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData)) {
    			//$cust_no = (int)$form->getValue('cust_no');
    			$cust_title = $form->getValue('cust_title');
    			$cust_first_name = $form->getValue('cust_first_name');
    			$cust_last_name = $form->getValue('cust_last_name');
    			$cust_address = $form->getValue('cust_address');
    			$cust_suburb = $form->getValue('cust_suburb');
    			$cust_state = $form->getValue('cust_state');
    			if ($cust_state == '') {
    				$cust_state = null;
    			}
    			$cust_post_code = $form->getValue('cust_post_code');
    			if ($cust_post_code == '') {
    				$cust_post_code = null;
    			}
    			$cust_home_phone = $form->getValue('cust_home_phone');
    			if ($cust_home_phone == '') {
    				$cust_home_phone = null;
    			}
    			$cust_work_phone = $form->getValue('cust_work_phone');
    			if ($cust_work_phone == '') {
    				$cust_work_phone = null;
    			}
    			$cust_mobile_phone = $form->getValue('cust_mobile_phone');
    			if ($cust_mobile_phone == '') {
    				$cust_mobile_phone = null;
    			}
    			$cust_pay_method = $form->getValue('cust_pay_method');
    			
    			if ($cust_home_phone == null && $cust_work_phone == null && $cust_mobile_phone == null) {
    				$this->view->phone_checker = false;
    			} else {
    				$customers = new Application_Model_DbTable_Customers();
    				$customers->addCustomer($cust_title, $cust_first_name, $cust_last_name, $cust_address, $cust_suburb, $cust_state, $cust_post_code, $cust_home_phone, $cust_work_phone, $cust_mobile_phone, $cust_pay_method);
    				 
    				//$this->_forward('review', 'customer');
    				$this->_helper->redirector('review', 'customer');
    			}
    			
    			
    			//$customers = new Application_Model_DbTable_Customers();
    			//$customers->addCustomer($cust_title, $cust_first_name, $cust_last_name, $cust_address, $cust_suburb, $cust_state, $cust_post_code, $cust_home_phone, $cust_work_phone, $cust_mobile_phone);
    			
    			//$this->_forward('review', 'customer');
    		} else {
    			$form->populate($formData);
    		}
    	}
    	
    }

    public function reviewAction()
    {
    	$request = $this->getRequest();
    	$cust_no = $request->getParam('cust_no');
    	$this->view->assign('cust_no', $cust_no);
    	
    	if (isset($cust_no)) {
    		$customers = new Application_Model_DbTable_Customers();
    		$select = $customers->select()
    			->limit(1)
    			->where('cust_no = ?', $cust_no);
    		$this->view->customers = $customers->fetchRow($select);
    	} else {
    		$customers = new Application_Model_DbTable_Customers();
    		$select = $customers->select()
    			->limit(1)
    			->order('cust_no DESC');
    		$this->view->customers = $customers->fetchRow($select);
    	}
    }

    public function editAction()
    {
        $form = new Application_Form_Customer();
        $form->submit->setLabel('Save');
        
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
        	$formData = $this->getRequest()->getPost();
        	if ($form->isValid($formData)) {
        		$cust_no = (int)$form->getValue('cust_no');
        		
        		$cust_title = $form->getValue('cust_title');
        		$ck_cust_title = $form->getValue('ck_cust_title');
        		if (md5($cust_title) == $ck_cust_title) {
        			$cust_title = false; // -> Not changed, so do not update.
        		}
        		
        		$cust_first_name = $form->getValue('cust_first_name');
        		$ck_cust_first_name = $form->getValue('ck_cust_first_name');
        		if (md5($cust_first_name) == $ck_cust_first_name) {
        			$cust_first_name = false;
        		}
				
        		$cust_last_name = $form->getValue('cust_last_name');
        		$ck_cust_last_name = $form->getValue('ck_cust_last_name');
        		if (md5($cust_last_name) == $ck_cust_last_name) {
        			$cust_last_name = false;
        		}
        		
        		$cust_address = $form->getValue('cust_address');
        		$ck_cust_address = $form->getValue('ck_cust_address');
        		if (md5($cust_address) == $ck_cust_address) {
        			$cust_address = false;
        		}
        		
        		$cust_suburb = $form->getValue('cust_suburb');
        		$ck_cust_suburb = $form->getValue('ck_cust_suburb');
        		if (md5($cust_suburb) == $ck_cust_suburb) {
        			$cust_suburb = false;
        		}
        		
        		$cust_state = $form->getValue('cust_state');
        		$ck_cust_state = $form->getValue('ck_cust_state');
        		if (md5($cust_state) == $ck_cust_state) {
        			$cust_state = false;
        		}
        		
        		$cust_post_code = $form->getValue('cust_post_code');
        		$ck_cust_post_code = $form->getValue('ck_cust_post_code');
        		if (md5($cust_post_code) == $ck_cust_post_code) {
        			$cust_post_code = false;
        		}
        		
        		$cust_home_phone = $form->getValue('cust_home_phone');
        		$ck_cust_home_phone = $form->getValue('ck_cust_home_phone');
        		if (md5($cust_home_phone) == $ck_cust_home_phone) {
        			$cust_home_phone= false;
        		}
        		//if ($cust_home_phone == '') {
        		//	$cust_home_phone = null;
        		//}
        		
        		$cust_work_phone = $form->getValue('cust_work_phone');
        		$ck_cust_work_phone = $form->getValue('ck_cust_work_phone');
        		if (md5($cust_work_phone) == $ck_cust_work_phone) {
        			$cust_work_phone = false;
        		}
        		//if ($cust_work_phone == '') {
        		//	$cust_work_phone = null;
        		//}
        		
        		$cust_mobile_phone = $form->getValue('cust_mobile_phone');
        		$ck_cust_mobile_phone = $form->getValue('ck_cust_mobile_phone');
        		if (md5($cust_mobile_phone) == $ck_cust_mobile_phone) {
        			$cust_mobile_phone = false;
        			$test1 = md5($cust_mobile_phone);
        			$test2 = $ck_cust_mobile_phone;
        		}
        		//if ($cust_mobile_phone == '') {
        		//	$cust_mobile_phone = null;
        		//}
        		
        		$cust_pay_method = $form->getValue('cust_pay_method');
        		$ck_cust_pay_method = $form->getValue('ck_cust_pay_method');
        		if (md5($cust_pay_method) == $ck_cust_pay_method) {
        			$cust_pay_method = false;
        		}
        		
        		$customers = new Application_Model_DbTable_Customers();
        		$customers->updateCustomer($cust_no, $cust_title, $cust_first_name, $cust_last_name, $cust_address, $cust_suburb, $cust_state, $cust_post_code, $cust_home_phone, $cust_work_phone, $cust_mobile_phone, $cust_pay_method);
        		
        		$param = array('cust_no'=>$cust_no);
        		$this->_helper->redirector('review', 'customer', null, $param);
        	} else {
        		$form->populate($formData);
        	}
        } else {
        		$cust_no = $this->_getParam('cust_no', 0);
        		if ($cust_no > 0) {
        			$customers = new Application_Model_DbTable_Customers();
        			$cust_list = $customers->getCustomer($cust_no);
        			$form->populate($cust_list);
        			
        			$ck_cust_title = md5($cust_list['cust_title']);
        			$ck_cust_first_name = md5($cust_list['cust_first_name']);
        			$ck_cust_last_name = md5($cust_list['cust_last_name']);
        			$ck_cust_address = md5($cust_list['cust_address']);
        			$ck_cust_suburb = md5($cust_list['cust_suburb']);
        			$ck_cust_state = md5($cust_list['cust_state']);
        			$ck_cust_post_code = md5($cust_list['cust_post_code']);
        			$ck_cust_home_phone = md5($cust_list['cust_home_phone']);
        			$ck_cust_work_phone = md5($cust_list['cust_work_phone']);
        			$ck_cust_mobile_phone = md5($cust_list['cust_mobile_phone']);
        			$ck_cust_pay_method = md5($cust_list['cust_pay_method']);
        			
        			$form->ck_cust_title->setValue($ck_cust_title);
        			$form->ck_cust_first_name->setValue($ck_cust_first_name);
        			$form->ck_cust_last_name->setValue($ck_cust_last_name);
        			$form->ck_cust_address->setValue($ck_cust_address);
        			$form->ck_cust_suburb->setValue($ck_cust_suburb);
        			$form->ck_cust_state->setValue($ck_cust_state);
        			$form->ck_cust_post_code->setValue($ck_cust_post_code);
        			$form->ck_cust_home_phone->setValue($ck_cust_home_phone);
        			$form->ck_cust_work_phone->setValue($ck_cust_work_phone);
        			$form->ck_cust_mobile_phone->setValue($ck_cust_mobile_phone);
        			$form->ck_cust_pay_method->setValue($ck_cust_pay_method);
        			
        		}
        }
    }
    
        
    public function enquiryAction()
    {
    	$form = new Application_Form_Cenquiry();
    	$form->submit->setLabel('Search');
    	$this->view->form = $form;
    	 
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData)) {
    			$cust_no = (int)$form->getValue('cust_no');
    			$cust_first_name = $form->getValue('cust_first_name');
    			$cust_last_name = $form->getValue('cust_last_name');
    			$cust_suburb = $form->getValue('cust_suburb');
    			$cust_post_code = $form->getValue('cust_post_code');
    			$cust_phone = $form->getValue('cust_phone');
    			 
    			//$params = array();
    			 
    			if ($cust_no != null){
    				$params['cust_no'] = $cust_no;
    			}
    			if ($cust_first_name != null){
    				$params['cust_first_name'] = $cust_first_name;
    			}
    			if ($cust_last_name != null) {
    				$params['cust_last_name'] = $cust_last_name;
    			}
    			if ($cust_suburb != null) {
    				$params['cust_suburb'] = $cust_suburb;
    			}
    			if ($cust_post_code != null) {
    				$params['cust_post_code'] = $cust_post_code;
    			}
    			if ($cust_phone != null) {
    				$params['cust_phone'] = $cust_phone;
    			}
    			
    			if (isset($params)) {
    				$this->_helper->redirector('enquiry', 'customer', null, $params);
    			} else {
    				$this->_helper->redirector('enquiry', 'customer');
    			}
    			 
    		} else {
    			$form->populate($formData);
    		}
    	} else {
    		$request = $this->getRequest();
    		
    		$search = null;
    		
    		$cust_no = (int)$request->getParam('cust_no');
    		if ($cust_no != 0){
    			$form->cust_no->setValue($cust_no);
    			$search = $this->search('cust_no', $cust_no, $search);
    		}
    
    		$cust_first_name = $request->getParam('cust_first_name');
    		if ($cust_first_name != null) {
    			$form->cust_first_name->setValue($cust_first_name);
    			$search = $this->search('cust_first_name', $cust_first_name, $search);
    		}
    		$cust_last_name = $request->getParam('cust_last_name');
    		if ($cust_last_name != null) {
    			$form->cust_last_name->setValue($cust_last_name);
    			$search = $this->search('cust_last_name', $cust_last_name, $search);
    		}
    		$cust_suburb = $request->getParam('cust_suburb');
    		if ($cust_suburb != null) {
    			$form->cust_suburb->setValue($cust_suburb);
    			$search = $this->search('cust_suburb', $cust_suburb, $search);
    		}
    		$cust_post_code = $request->getParam('cust_post_code');
    		if ($cust_post_code != null) {
    			$form->cust_post_code->setValue($cust_post_code);
    			$search = $this->search('cust_post_code', $cust_post_code, $search);
    		}
    		$cust_phone = $request->getParam('cust_phone');
    		if ($cust_phone != null) {
    			$form->cust_phone->setValue($cust_phone);
    			if ($search != null) {
    				$search = $search . ' AND ( cust_home_phone Like "%' . $cust_phone . '%" OR cust_work_phone Like "%' . $cust_phone . '%" OR cust_mobile_phone Like "%' . $cust_phone . '%" )';
    			} else {
    				$search = 'cust_home_phone Like "%' . $cust_phone . '%" OR cust_work_phone Like "%'
    					. $cust_phone . '%" OR cust_mobile_phone Like "%' . $cust_phone . '%"';
    			}
    		}

    		$customers = new Application_Model_DbTable_Customers();
    		if ($search != null) {
    			$select = $customers->select()
    				->where($search);
    		
    			$this->view->customers = $customers->fetchAll($select);
    		} else {
    			$this->view->customers = $customers->fetchAll();
    		}
    	}
    	
    }

    public function search($name, $param, $search)
    {
    	if ($search != null) {
    		if (is_numeric($param)) {
    			//$search = $search . ' AND ' . $name . ' = ' . $param;
    			$search = $search . ' AND ' . $name . ' Like "%' . $param . '%"';
    		} else {
    			//$search = $search . ' AND ' . $name . ' = "' . $param . '"';
    			$search = $search . ' AND ' . $name . ' Like "%' . $param . '%"';
    		}
    	} else {
    		if (is_numeric($param)) {
    			//$search = $name . ' = ' . $param;
    			$search = $name . ' Like "%' . $param . '%"';
    		} else {
    			//$search = $name . ' = "' . $param . '"';
    			$search = $name . ' Like "%' . $param . '%"';
    		}
    	}
    	return $search;
    }

    public function requestAction()
    {
        // action body
    	
    	$cust_no = $this->_getParam('cust_no', 0);
    	$customer = new Application_Model_DbTable_Customers();
    	
    	
    	$this->_helper->redirector('enquiry', 'customer');
    }


}
  